package com.saumya.utils 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.FileListEvent;
	import flash.events.TimerEvent;
	import flash.events.ProgressEvent;
	import flash.events.NativeProcessExitEvent;
	import flash.events.IOErrorEvent;
	//
	import flash.filesystem.File;
	//
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	
	/**
	 * Gets data aboout files from SVN
	 * 
	 * @author saumya
	 * @version 0.1
	 */
	public class SvnUtil extends EventDispatcher
	{
		private var callee:Main = null;
		private var rawFiles:Array = [];
		//
		private var currentFile:File = null;
		private var _fileVO:FileVO = new FileVO();
		//
		public static const OUTPUT_EVENT:String = 'onOutputDataEvent';
		
		public function SvnUtil(calledFrom:Main) 
		{
			this.callee = calledFrom;
		}
		
		/*
		public function getInfo(filesArray:Array=null):void
		{
			this.rawFiles = [];
			this.rawFiles = filesArray;
		}
		*/
		
		/**
		 * 
		 * @param	fileObj
		 */
		public function getFileInfo(fileObj:File):void
		{
			this.checkSVNProcess(fileObj);
		}
		
		/**
		 * This exposed method is just to test the utility from every where else
		 */
		public function testSVN():void
		{
			this.checkSVNProcess();
		}
		
		private function checkSVNProcess(fileRefObject:File=null):void
		{
			if (fileRefObject!=null)
			{
				this.currentFile = fileRefObject;
				
				this.callee.log(' : checkSVNProcess : fileRef : folder=' + fileRefObject.parent.url);
				this.callee.log(' : checkSVNProcess : fileRef : file=' + fileRefObject.name);
				
				var exeFile:File = new File("file:///C://Program Files//CollabNet//Subversion Client//svn.exe");
				var nativePSInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
				nativePSInfo.executable = exeFile;
				nativePSInfo.workingDirectory = new File(fileRefObject.parent.url);
				
				var processArgsV:Vector.<String> = new Vector.<String>();
				//---------------------svn---------------------------------------------
				processArgsV[0] = "info";
				processArgsV[1] = fileRefObject.name;
				
				nativePSInfo.arguments = processArgsV;
				//
				var nProcess:NativeProcess = new NativeProcess();
				//
				nProcess.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, inputProgressListener); 
				nProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
				nProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
				nProcess.addEventListener(NativeProcessExitEvent.EXIT, onExit);
				nProcess.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
				nProcess.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
				//
				nProcess.start(nativePSInfo); 
			} else {
				this.currentFile = new File('file:///C://saumya//projects//wel6//wel6Build//trunk//wel6x//src//dev//app_fla//WStage.fla');
				
				//default check
				this.callee.log(' SvnUtil : DEFAULT : SVN check : ');
				var file:File = new File("file:///C://Program Files//CollabNet//Subversion Client//svn.exe");
				//
				var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
				nativeProcessStartupInfo.executable = file;
				nativeProcessStartupInfo.workingDirectory = new File('file:///C://saumya//projects//wel6//wel6Build//trunk//wel6x//src//dev//app_fla');
				
				var processArgs:Vector.<String> = new Vector.<String>();
				//---------------------svn---------------------------------------------
				//processArgs[0] = "--version";
				processArgs[0] = "info";
				//processArgs[1] = "file:///C://saumya//projects//wel6//repository//repo_01//trunk//wel6x//src//dev//app_fla//WStage.fla";
				processArgs[1] = "WStage"+".fla";
				
				nativeProcessStartupInfo.arguments = processArgs;
				//
				var process:NativeProcess = new NativeProcess();
				
				process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, inputProgressListener); 
				process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
				process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
				process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
				process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
				process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
				//
				process.start(nativeProcessStartupInfo); 
			}
		}
		
		private function inputProgressListener(e:ProgressEvent):void 
		{
			this.callee.log(' SvnUtil : inputProgressListener : ProgressEvent : ');
		}
		
		private function onIOError(e:IOErrorEvent):void 
		{
			this.callee.log(' SvnUtil : onIOError : IOErrorEvent : ');
		}
		
		private function onExit(e:NativeProcessExitEvent):void 
		{
			this.callee.log(' SvnUtil : onExit : ');
		}
		
		private function onErrorData(e:ProgressEvent):void 
		{
			//this.callee.log(' : onErrorData : ProgressEvent : ');
			//this.callee.log(' : onErrorData : ProgressEvent : '+e.toString());
			
			var np:NativeProcess = NativeProcess(e.target);
			var ba:uint = np.standardError.bytesAvailable;
			var error:String = np.standardError.readUTFBytes(ba);
			this.callee.log(error);
			
		}
		
		private function onOutputData(e:ProgressEvent):void 
		{
			this.callee.log(' SvnUtil : onOutputData : ');
			var np:NativeProcess = NativeProcess(e.target);
			var ba:uint = np.standardOutput.bytesAvailable;
			var outputString:String = np.standardOutput.readUTFBytes(ba);
			this.callee.log(outputString, true);
			//this.callee.log('------------getting the date----------');
			/*
			for (var i:uint = 0; i < outputString.length; i++ )
			{
				this.callee.log('------------------'+i.toString()+' : '+outputString.charAt(i));
			}
			*/
			//this.callee.log(outputString);
			//TODO : get the file name from callee.log and check against all the files stored in the array for date
			//TODO : then make another array of the updated files only
			
			//gets the date of the file
			this.callee.log('SvnUtil : '+outputString.indexOf("Last Changed Date:").toString());
			var startIndex:Number = outputString.indexOf("Last Changed Date:");
			
			//this.callee.log(outputString.substring(371,381));
			//this.callee.log(outputString.substring(startIndex + 19, startIndex + 29)); //gets the date
			var yearString:String = outputString.substring(startIndex + 19, startIndex + 23);
			var monthString:String = outputString.substring(startIndex + 24, startIndex + 26);
			var dayString:String = outputString.substring(startIndex + 27, startIndex + 29);
			this.callee.log(' SvnUtil : ' + this.currentFile.url + ' : ' + dayString + ' : ' + monthString + ' : ' + yearString, true);
			//
			//since Flash saves months from 0 count instead of 1, we need to decrement one day to store
			var flashMonth:int = int(monthString)-1;
			var revDate:Date = new Date(yearString, flashMonth, dayString);
			
			//
			this.fileVO.file = this.currentFile;
			this.fileVO.lastCommitted = revDate;
			//this.fileVO.lastCommitted=
			var evt:Event = new Event(SvnUtil.OUTPUT_EVENT);
			this.dispatchEvent(evt);
		}
		
		public function get fileVO():FileVO { return _fileVO; }
		
		public function set fileVO(value:FileVO):void 
		{
			_fileVO = value;
		}
		
		
		
		
	}

}